<template>
  <div
    class="swiper-container"
    ref="floor1Swiper"
    :style="{ height: height, width: width }"
  >
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(carousel, index) in list"
        :key="carousel.id"
        @click="goNewsDetail(carousel)"
      >
        <img :src="carousel.url" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
export default {
  name: "Carousel",
  props: ["list", "wid", "hei"],
  data() {
    return {
      width: this.wid + "px",
      height: this.hei + "px",
      userInfo: {
        name: "",
        money: 0,
        page: "",
        phone: "",
        sex: "",
        state: "",
        password: "",
      },
    };
  },
  watch: {
    // 父亲给的数据没有变化 监听不到
    list: {
      // 上来立即监听
      immediate: true,
      handler(newValue, oldValue) {
        this.$nextTick(() => {
          var mySwiper = new Swiper(this.$refs.floor1Swiper, {
            // 循环模式选项
            loop: true,
            // 自动播放
            autoplay: {
              delay: 1500,
              stopOnLastSlide: false, //如果设置为true，当切换到最后一个slide时停止自动切换
              disableOnInteraction: true,
            },

            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
  mounted() {
    this.getUserInfo();
  },
  methods: {
    getUserInfo() {
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
    },
    goNewsDetail(carousel) {
      this.$router.push({
        path: "/newsdetail",
        query: { id: carousel.id, username: this.userInfo.name },
      });
    },
  },
};
</script>

<style></style>
